using System;
using TimeSheetBLL;

namespace TimeSheet
{
	/// <summary>
	/// A nice description of BaseControl goes here.
	/// 
	/// Version: $Id$
	/// 
	/// Date: 
	/// Copyright: 2004, Kanga Communications, Calgary, Alberta, Canada. 
	/// </summary>
	public class BasePage : System.Web.UI.Page
	{
		public User GetActiveUser()
		{
			User user = Session[ "User" ] as User;
			if( user == null && Request[ "id" ] != null )
			{
				if( GetCurrentAdministrator() != null )
				{
					return GetUserFromRequest();
				}
			}
			else if( user != null && Request[ "id" ] != null )
			{
				if( GetCurrentAdministrator() != null )
				{
					long id = Convert.ToInt64( Request[ "id" ] );
					if( id != user.ID )
					{
						return GetUserFromRequest();
					}
				}
			}
			return user;
		}

		protected User GetUserFromRequest()
		{
			User user = new User();
			user = new User();
			user.ID = Convert.ToInt64( Request[ "id" ] );
			BrokerFactory.Fill( user );
			Session[ "User" ] = user;
			return user;
		}


		protected virtual TimeSheetBLL.Administrator GetCurrentAdministrator()
		{
			return this.Session[ "Administrator" ] as TimeSheetBLL.Administrator;
		}
	}
}
